package com.htjx.mall.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.htjx.mall.admin.controllers.vo.CategoryQuery;
import com.htjx.mall.commons.mappers.CategoryMapper;
import com.htjx.mall.commons.models.enums.SeqType;
import com.htjx.mall.commons.models.product.Category;
import com.htjx.mall.commons.utils.page.Page;

@Service
public class CategoryService {
	@Autowired
	private CategoryMapper categoryMapper; 
	@Autowired
	private SequenceManager sequenceManager;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	public List<Category> selectByActive(boolean active) {
		return categoryMapper.selectByActive(active);
	}
	public void insertOne(final Category category) {
		long[] seq = sequenceManager.buildBatchSeq(SeqType.Category, 1);
		category.setId(seq[0]);
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				categoryMapper.insertOne(category);
			}
		});
	}
	
	public Category selectById(long id) {
		return categoryMapper.selectOne(id);
	}
	
	public void updateOne(final Category category) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				categoryMapper.updateOne(category);
			}
		});
	}
	
	public void deleteOne(final long id) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				categoryMapper.deleteOne(id);
			}
		});
	}
	
	public Page<Category> page(CategoryQuery categoryQuery, Page<Category> page) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("offset", page.getFirst() - 1);
		params.put("size", page.getPageSize());
		params.put("categoryQuery", categoryQuery);
		List<Category> result = categoryMapper.page(params);
		page.setResult(result);
		if (page.isAutoCount()) {
			int totalCount = categoryMapper.pageCount(params);
			page.setTotalCount(totalCount);
		}
		return page;
	}
	
	/**
	 * 检测唯一属性
	 */
	public boolean checkUniqueByProperty(String propertyName, String propertyValue) {
		return !categoryMapper.selectByProperty(propertyName, propertyValue).isEmpty();
	}
	
	public Category findUniqueBy(String propertyName, String propertyValue) {
		List<Category> list = categoryMapper.selectByProperty(propertyName, propertyValue);
		if (list.isEmpty()) {
			return null;
		}
		if (list.size() == 1) {
			return list.get(0);
		}
		throw new RuntimeException("there are too many rows, actual " + list.size());
	}
}
